package com.xkxx.task.model;

import lombok.Data;

import java.io.Serializable;

/**
 * @version V1.0
 * @Description 通用返回对象
 */
@Data
public class ResultApi<T> implements Serializable {

	/**
	 * @Fields serialVersionUID :
	 */
	private final static long serialVersionUID = -6327861644098424604L;

	/**
	 * @Fields DEFAULT_SUCCESS_CODE : 默认正确码
	 */
	private final static Integer DEFAULT_SUCCESS_CODE = 0;

	/**
	 * @Fields DEFAULT_ERROR_CODE : 默认错误码
	 */
	private final static Integer DEFAULT_ERROR_CODE = 1;

	private Boolean success = false;

	private String msg;

	private Integer code;

	private boolean hasNextPage = false;

	private long total;

	private T obj;

	/**
	 * @Description 成功
	 * @return ResultApi<T> 通用返回对象
	 */
	public static <T> ResultApi<T> instanceSuccess() {
		return instanceSuccess(null, false);
	}

	/**
	 * @Description 成功
	 * @param t 返回对象
	 * @return ResultApi<T> 通用返回对象
	 */
	public static <T> ResultApi<T> instanceSuccess(T t) {
		return instanceSuccess(t, false);
	}

	/**
	 * @Description 成功
	 * @param t           返回对象
	 * @param hasNextPage 是否有下页
	 * @return 通用返回对象
	 */
	public static <T> ResultApi<T> instanceSuccess(T t, Boolean hasNextPage) {
		ResultApi<T> resultApi = new ResultApi<>();
		resultApi.setSuccess(true);
		resultApi.setCode(DEFAULT_SUCCESS_CODE);
		resultApi.setHasNextPage(hasNextPage);
		resultApi.setObj(t);
		return resultApi;
	}

	/**
	 * @Description 成功
	 * @param t     返回对象
	 * @param total 总记录数
	 * @return 通用返回对象
	 */
	public static <T> ResultApi<T> instanceSuccess(T t, long total) {
		ResultApi<T> resultApi = new ResultApi<>();
		resultApi.setSuccess(true);
		resultApi.setCode(DEFAULT_SUCCESS_CODE);
		resultApi.setTotal(total);
		resultApi.setObj(t);
		return resultApi;
	}

	/**
	 * @Description 错误
	 * @param msg 错误信息
	 * @return ResultApi<T> 通用返回对象
	 */
	public static <T> ResultApi<T> instanceError(String msg) {
		return instanceError(msg, DEFAULT_ERROR_CODE);
	}

	/**
	 * @Description 错误
	 * @param msg  错误信息
	 * @param code 返回码
	 * @return ResultApi<T> 通用返回对象
	 */
	public static <T> ResultApi<T> instanceError(String msg, Integer code) {
		ResultApi<T> resultApi = new ResultApi<>();
		resultApi.setSuccess(false);
		resultApi.setCode(code);
		resultApi.setMsg(msg);
		return resultApi;
	}
}
